﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Solver;

namespace ChisMetLab5
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void radioButton2_CheckedChanged(object sender, EventArgs e)
    {
    }

    private void radioButton3_CheckedChanged(object sender, EventArgs e)
    {
    }

    private void radioButton1_CheckedChanged(object sender, EventArgs e)
    {
    }

    private void button1_Click(object sender, EventArgs e)
    {
      dataGridView1.Rows.Clear();
      Solver.Solver s;
      s = new Solver.Solver();

      int curStep = 0;
      double curMaxEps = 0;

      int n = Convert.ToInt32(textBox1.Text);
      int m = Convert.ToInt32(textBox2.Text);

      double h = 2 / Convert.ToDouble(n);
      double k = 2 / Convert.ToDouble(m);

      int stepLimit = Convert.ToInt32(textBox5.Text);
      double Eps = Convert.ToDouble(textBox4.Text);

      double[,] v = new double[n + 1, m + 1];
      double[,] u = new double[n + 1, m + 1];
      double[,] f = new double[n + 1, m + 1];

      if (radioButton2.Checked)
      {
        // граничные и начальные условия
        for (int i = 0; i < n + 1; i++)
        {
          u[i, 0] = v[i, 0] = -(-1 + h * i) * (-1 + h * i);
          u[i, m] = v[i, m] = -(-1 + h * i) * (-1 + h * i);


          for (int j = 1; j < m; j++)
          {
            f[i, j] = 4;
            v[i, j] = 0;
            u[i, j] = 1 - (-1 + i * h) * (-1 + i * h) - (-1 + j * k) * (-1 + j * k);
          }
        }

        for (int j = 0; j < m + 1; j++)
        {
          u[0, j] = v[0, j] = -(-1 + k * j) * (-1 + k * j);
          u[n, j] = v[n, j] = -(-1 + k * j) * (-1 + k * j);
        }

        s.compute(v, f, h, k, n, m, out curStep, stepLimit, out curMaxEps, Eps);


        double uMinusV = 0.0;
        for (int i = 0; i <= n; i++)
          for (int j = 0; j <= m; j++)
          {
            if (Math.Abs(u[i, j] - v[i, j]) > uMinusV)
              uMinusV = Math.Abs(u[i, j] - v[i, j]);
          }
        textBox3.Text = uMinusV.ToString("E2");
        textBox6.Text = curStep.ToString("G6");
        textBox7.Text = curMaxEps.ToString("E2");

        #region вывод таблиц
        if (checkBox2.Checked)
        {
          int rowsCount = 0;
          for (int i = 0; i <= n; i++)
          {
            for (int j = 0; j <= m; j++)
            {
              dataGridView1.Rows.Add();
              dataGridView1.Rows[rowsCount].Cells[0].Value = j.ToString("G6");
              dataGridView1.Rows[rowsCount].Cells[1].Value = i.ToString("G6");
              dataGridView1.Rows[rowsCount].Cells[2].Value = v[i, j].ToString("E2");
              dataGridView1.Rows[rowsCount].Cells[3].Value = u[i, j].ToString("E2");
              dataGridView1.Rows[rowsCount].Cells[4].Value = Math.Abs(u[i, j] - v[i, j]).ToString("E2");
              rowsCount++;
            }
          }
        }
        #endregion
      }

      if (radioButton3.Checked)
      {
        // граничные и начальные условия
        for (int i = 0; i < n + 1; i++)
        {
          u[i, 0] = v[i, 0] = -(-1 + h * i) * (-1 + h * i);
          u[i, m] = v[i, m] = -(-1 + h * i) * (-1 + h * i);


          for (int j = 1; j < m; j++)
          {
            f[i, j] = 4;
            v[i, j] = 0;
            u[i, j] = 1 - (-1 + i * h) * (-1 + i * h) - (-1 + j * k) * (-1 + j * k);
          }
        }

        for (int j = 0; j < m + 1; j++)
        {
          u[0, j] = v[0, j] = -(-1 + k * j) * (-1 + k * j);
          u[n, j] = v[n, j] = -(-1 + k * j) * (-1 + k * j);
        }

        for (int i = 0; i <= n / 2; i++)
        {
          for (int j = 0; j <= m / 2; j++)
          {
            v[i, j] = u[i, j] = 1 - (-1 + i * h) * (-1 + i * h) 
              - (-1 + j * k) * (-1 + j * k);
          }

        }

        s.compute2(v, f, h, k, n, m, out curStep, stepLimit, out curMaxEps, Eps);


        double uMinusV = 0.0;
        for (int i = 0; i <= n; i++)
          for (int j = 0; j <= m; j++)
          {
            if (Math.Abs(u[i, j] - v[i, j]) > uMinusV)
              uMinusV = Math.Abs(u[i, j] - v[i, j]);
          }
        textBox3.Text = uMinusV.ToString("E2");
        textBox6.Text = curStep.ToString("G6");
        textBox7.Text = curMaxEps.ToString("E2");

        #region вывод таблиц
        if (checkBox2.Checked)
        {
          int rowsCount = 0;
          for (int i = 0; i <= n; i++)
          {
            for (int j = 0; j <= m; j++)
            {
              if ((j < m / 2) && (i < n / 2))
                continue;
              dataGridView1.Rows.Add();
              dataGridView1.Rows[rowsCount].Cells[0].Value = j.ToString("G6");
              dataGridView1.Rows[rowsCount].Cells[1].Value = i.ToString("G6");
              dataGridView1.Rows[rowsCount].Cells[2].Value = v[i, j].ToString("E2");
              dataGridView1.Rows[rowsCount].Cells[3].Value = u[i, j].ToString("E2");
              dataGridView1.Rows[rowsCount].Cells[4].Value = Math.Abs(u[i, j] - v[i, j]).ToString("E2");
              rowsCount++;
            }
          }
        }
        #endregion
      }

    }
  }
}
